package org.pmt.first.kms.screen

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import moe.tlaster.precompose.molecule.producePresenter

@Composable
fun NewsScreen() {
    val state by producePresenter { NewsPresenter() }
    MaterialTheme {
        Surface(
            modifier = Modifier.fillMaxSize(),
            color = MaterialTheme.colorScheme.background
        ){
            Column(
                modifier = Modifier.fillMaxSize(),
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = Arrangement.Center,
            ) {
                Text(text = state.count)
                Button(
                    onClick = {
                        state.action(NewsAction.InitData)
                    },
                ) {
                    Text(text = "Increment")
                }
                Button(
                    onClick = {
                        state.action(NewsAction.InitData)
                    },
                ) {
                    Text(text = "Decrement")
                }
            }
        }
    }
}

@Composable
fun NewsPresenter(): NewsState {
    var count by remember { mutableStateOf(0) }
    return NewsState(
        "Clicked $count times",
    ) {
        when (it) {
            NewsAction.InitData -> count++
            NewsAction.LoadMore -> count--
             else -> {}
        }
    }
}

sealed interface NewsAction {
    object InitData : NewsAction
    object LoadMore : NewsAction
}

data class NewsState(
    val count: String,
    val action: (NewsAction) -> Unit,
)